const config=require('./config.js');
                    
  module.exports = {
    // formatTime: formatTime,
    show_loading: show_loading,
    hide_loading: hide_loading,
    request: request,
    upload_file: upload_file
  }

//   const formatTime = date => {
//   const year = date.getFullYear()
//   const month = date.getMonth() + 1
//   const day = date.getDate()
//   const hour = date.getHours()
//   const minute = date.getMinutes()
//   const second = date.getSeconds()
//   return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map              (formatNumber).join(':')  
// }

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function show_loading() {
  wx.showLoading({
    title: '请稍等',
  })
}

function hide_loading() {
  wx.hideLoading();
}

function request(url, data, callback) {
  wx.request({
    url: config.root_url + url,
    data: data,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    success: function (res) {
      // console.log(url,res)
      typeof callback == 'function' && callback(res.data);
      if (res.data.ret!= '0') {
        console.info("util_request:" + url + "-->" + JSON.stringify(res.data));
      }
    },
    fail: function (res) {
      if (wx.hideLoading()) {
        wx.hideLoading();
      }
      wx.showToast({
        title: '网络延迟',
        duration: 2000,
        mask: true,
      })
      // setTimeout(function () {
      //   request(url, data, callback)
      // }, 10000)
      console.error("wx_request_failed:" + url + "-->" + JSON.stringify(res));
    },
    complete: function (res) {

    }
  });
}

function upload_file(filePath, data, callback) {
  wx.uploadFile({
    url: config.root_url + '/app/upload_file',
    filePath: filePath,
    name: 'imgFile',
    formData: data,
    success: function (res) {
      typeof callback == 'function' && callback(JSON.parse(res.data));
    }
  })
}
